import React, { Component } from 'react'
import {HashRouter as Router, Route} from 'react-router-dom'
import Films from '../views/Films'
import Cinemas from '../views/Cinemas'
import Me from '../views/Me'
import Detail from '../views/Detail'
import Login from '../views/Login'
import City from '../views/City'
import { Redirect } from 'react-router-dom'
import { Switch } from 'react-router-dom'
import Notfond from '../views/Notfond'
import Search from '../views/Search'

export default class index extends Component {
  isAuth(){
    // console.log(localStorage.getItem('token'));
    if(localStorage.getItem('token'))
      return 1;
    else
      return 0;
  }
  render() {
    return (
      //两种,Hashrouter和BrowserRouter，前者带#号，后者不带
        <Router>
         <Switch>    {/*防止刷新页面时候被重定向 */}
            <Route path='/films' component={Films}></Route>
            <Route path='/cinemas' component={Cinemas}></Route>
            <Route path='/me' render={()=>{
              return this.isAuth()?<Me/>:<Redirect to='/login'></Redirect>
            }}></Route>
            <Route path='/login' component={Login}></Route>
            <Route path='/search' component={Search}></Route>
            <Route path='/city' component={City}></Route>
            <Route path='/detail/:myid' component={Detail}></Route>   {/* 动态路由 */}
            <Redirect from='/' to='/films' exact></Redirect>   {/*exact精准定位，模糊定位将exact去掉即可 */}
            <Route component={Notfond}></Route>    {/*如果上方的路径全部没有匹配到，就跳转到Notfond组件*/}
        </Switch>
        {this.props.children}
      </Router>
    )
  }
}